package com.fishery.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.Germchit;
import com.fishery.base.service.GermchitService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 种苗前端控制器
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/germchit")
@CrossOrigin
@Api(tags = "种苗模块（偏向商家）")
public class GermchitController {

	@Autowired
	private GermchitService germchitService;


	/**
	 * 新增种苗信息
	 *
	 * @param germchit
	 * @return
	 */
	@PostMapping
	@ApiOperation("新增种苗信息")
	public Result saveGermchit(@RequestBody @ApiParam(value = "germchit对象", required = true) Germchit germchit) {
		if (germchit == null) {
			return Result.error();
		}
		return germchitService.save(germchit) ? Result.success("保存成功") : Result.error("保存失败");
	}


	/**
	 * 获取所有种苗信息
	 *
	 * @return
	 */
	@GetMapping
	@ApiOperation("获取所有种苗信息")
	public Result getAllGermchitInfo() {
		return Result.success().data(germchitService.list(null));
	}

	/**
	 * 获取所有种苗信息（带分页）
	 *
	 * @return
	 */
	@GetMapping("/{size}/{current}")
	@ApiOperation("获取所有种苗信息（带分页）")
	public Result getAllGermchitInfoWithPage(@PathVariable(name = "size", required = true) @ApiParam("尺寸") int size,
											 @PathVariable(name = "current", required = true) @ApiParam("页码") int current) {
		return Result.success().data(germchitService.page(new Page<Germchit>(current, size), null));
	}


	/**
	 * 通过种苗ID获取种苗信息
	 *
	 * @param gid
	 * @return
	 */
	@GetMapping("/{gid}")
	@ApiOperation("通过种苗ID获取种苗信息")
	public Result getGermchitInfo(@PathVariable(name = "gid", required = true) @ApiParam(value = "种苗ID", required = true) String gid) {
		if (StringUtils.isEmpty(gid)) {
			return Result.error("查询失败");
		}
		// 从数据库中查询种苗的信息
		return Result.success().data(germchitService.getById(gid));
	}


	/**
	 * 修改种苗信息
	 *
	 * @param germchit
	 * @return
	 */
	@PutMapping
	@ApiOperation("修改种苗信息")
	public Result updateGermchitInfo(@RequestBody @ApiParam(value = "germchit对象", required = true) Germchit germchit) {
		if (germchit == null || StringUtils.isEmpty(germchit.getId())) {
			return Result.error();
		}
		return germchitService.updateById(germchit) ? Result.success("修改成功") : Result.error("修改失败");
	}

	/**
	 * 通过种苗ID删除种苗信息
	 *
	 * @param gid
	 * @return
	 */
	@DeleteMapping("/{gid}")
	@ApiOperation("通过种苗ID删除种苗信息")
	public Result deleteGermchit(@PathVariable(name = "gid", required = true) @ApiParam(value = "gid", required = true) String gid) {
		if (StringUtils.isEmpty(gid)) {
			return Result.error("删除失败");
		}
		return germchitService.removeById(gid) ? Result.success("删除成功") : Result.error("删除失败");
	}


}
